package modellayer;
import java.util.ArrayList;

/**
 * The class ProductContainer is to collect objects of the class Product.
 * 
 * @author Allan Eriksen, Christian Stidsborg, Andreas Rye, Casper Madsen, Stefan Bols 
 * @version 051213
 */
public class ProductContainer
{
    private static ProductContainer instance;
    private ArrayList<Product> productList;

    /**
     * This function creates a Product container, only used when called by other functions
     */
    private ProductContainer()
    {
        productList = new ArrayList<Product>();
        
        // DUMMY TEST DATA:
        productList.add(new Product("97543288", 99.99, "Hammer", 32));
        productList.add(new Product("61765372", 49.99, "Cykelstativ", 35));
        productList.add(new Product("48946211", 19.99, "Sodavand", 12));
        productList.add(new Product("56901252", 14.99, "Vand", 12));
        productList.add(new Product("63428842", 356.99, "Elektrisk hammer", 20));
        productList.add(new Product("12345678", 1459.99, "Motorsav", 10));
    }
    
    /**
     * This is a singleton function by calling the ProductContainer method it creates a new container.
     * @return instance6
     * Instance is the new container, it can only be called once.
     */
    public static ProductContainer getInstance()
    {
        if (instance==null) 
        {
            instance = new ProductContainer();
        }
        
        return instance;
    }

    /**
     * This function searches for a product in the product container.
     * It utilizes a sweep/search pattern to find the object.
     * 
     * @param String barcode
     * @return returnProductCopy
     */
    public ProductCopy getProductCopy(String barcode)
    {
        int i = 0;
        boolean found = false;
        
        int listSize = productList.size();
        ProductCopy returnProductCopy = null;
        
        while(!found && i < listSize)
        {    
            if(barcode.equals(productList.get(i).getBarcode()))
            {
                found = true;
                returnProductCopy = productList.get(i).getProductCopy();
            }
            else
            { 
                i++;
            }
        }
        
        return returnProductCopy;
    }
    
    /**
     * This function searches for a product in the product container.
     * It utilizes a sweep/search pattern to find the object and then
     * return the price of it.
     * 
     * @param String barcode
     * @return double Price for the product
     */
    public double getProductPrice(String barcode)
    {
        boolean found = false;
        double returnPrice = 0;
        int listSize = productList.size();
        ProductCopy returnProductCopy = null;
        
        for (int i = 0; !found && i < listSize; i++)
        {    
            if(barcode.equals(productList.get(i).getBarcode()))
            {
                found = true;
                returnPrice = productList.get(i).getPrice();
            }
        }
        
        return returnPrice;
    }

    /*
     * This functions removes a product copy from the container
     * it utilizes a sweep/search pattern to find the object
     * @param int copyNo
     *
    public void removeProductCopy(int copyNo)
    {
        boolean done = false;
        int listSize = productList.size();
        
        for (int i = 0; 0 < listSize && !done; i++) 
        {
            if (productList.get(i).isCopyHere(copyNo)) 
            {
                productList.get(i).removeProductCopy(copyNo);
                done = true;
            }
        }
    }*/
    
    public Product getProductFromBarcode(String barcode)
    {
    	Product result = null;
    	boolean found = false;
    	int i = 0;
    	int listSize = productList.size();
    	
    	while (i<listSize && !found)
    	{
    		if (productList.get(i).getBarcode().equals(barcode))
    		{
    			result = productList.get(i);
    			found = true;
    		}
    		i++;
    	}
    	
    	return result;
    }
    
    public boolean deleteProductFromBarcode(String barcode)
    {
    	boolean found = false;
    	int i = 0;
    	int listSize = productList.size();
    	
    	while (i<listSize && !found)
    	{
    		if (productList.get(i).getBarcode().equals(barcode))
    		{
    			productList.remove(i);
    			found = true;
    		}
    		i++;
    	}
    	
    	return found;
    }
    
    public Product getProductFromProductNo(String productNo)
    {
    	Product result = null;
    	boolean found = false;
    	int i = 0;
    	int listSize = productList.size();
    	
    	while (i<listSize && !found)
    	{
    		if (productList.get(i).getProductNo().equals(productNo))
    		{
    			result = productList.get(i);
    			found = true;
    		}
    		i++;
    	}
    	
    	return result;
    }
    
    public ProductCopy getProductCopy(int copyNo, String barcode)
    {
    	ProductCopy result = null;
    	boolean found = false;
    	int i = 0;
    	int listSize = productList.size();
    	
    	while (i<listSize && !found)
    	{
    		if (getProductFromBarcode(barcode) != null)
    		{
    			result = getProductFromBarcode(barcode).getProductCopy(copyNo);
    			found = true;
    		}
    		i++;
    	}
    	
    	return result;
    }
    
    public ArrayList<Product> getAllProducts()
    {
    	ArrayList<Product> allProducts = null;
    	
    	for (Product p : productList)
    	{
    		allProducts.add(p);
    	}
    	    	   	
    	return allProducts;
    }
    
    public ArrayList<Product> getProductList()
    {
    	return productList;
    }
    
    public ArrayList<ProductCopy> getAllProductCopies(String barcode)
    {
    	ArrayList<ProductCopy> allProductCopies = getProductFromBarcode(barcode).getAllProductCopies();
    	
    	return allProductCopies;
    }
    
    public boolean addProduct(String barcode, double price, String productLabel, int numberOfCopies)
    {
    	Product p = new Product(barcode, price, productLabel, numberOfCopies);
    	
    	productList.add(p);
    	
    	return true;
    }
    
    public boolean addProductCopy(String barcode)
    {
    	Product p = getProductFromBarcode(barcode);
    	
    	p.addProductCopy();
    	
    	return true;
    }
}
